package cn.vawn.base.util;

import cn.vawn.base.dto.MASSubmitReq;
import cn.vawn.base.dto.MASSubmitRsp;
import cn.vawn.base.dto.MASTmpSubmitReq;
import org.apache.commons.codec.binary.Base64;

/**
 * Add by jianhan on 2018/5/11
 */
public class SMSUtil {

    /**
     * 移动-云MAS-短信消息推送
     *
     * @return
     */
    public static MASSubmitRsp sendSMSMASHttpSubmit(String mobiles, String content) {
        String apiUrl = PropertyUtil.getString("http.api.sms.mas.norsubmit");
        String apId = PropertyUtil.getString("sms.mas.apId");
        String ecName = PropertyUtil.getString("sms.mas.ecName");
        String secretKey = PropertyUtil.getString("sms.mas.secretKey");
        String sign = PropertyUtil.getString("sms.mas.sign");
        String addSerial = PropertyUtil.getString("sms.mas.addSerial");
        MASSubmitReq submitReq = new MASSubmitReq();
        submitReq.setApId(apId);
        submitReq.setEcName(ecName);
        submitReq.setSecretKey(secretKey);
        submitReq.setContent(content);
        submitReq.setMobiles(mobiles);
        submitReq.setAddSerial(addSerial);
        submitReq.setSign(sign);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(submitReq.getEcName());
        stringBuffer.append(submitReq.getApId());
        stringBuffer.append(submitReq.getSecretKey());
        stringBuffer.append(submitReq.getMobiles());
        stringBuffer.append(submitReq.getContent());
        stringBuffer.append(submitReq.getSign());
        stringBuffer.append(submitReq.getAddSerial());
        submitReq.setMac(EncryptUtil.md5(stringBuffer.toString()));
        // 参数转成json字符串
        String reqText = JsonUtil.toJson(submitReq);
        // 参数转成base64
        String encode = Base64.encodeBase64String(reqText.getBytes());
        try {
            String resString = HttpClientUtil.doPostWithJson(apiUrl, encode);
            MASSubmitRsp resMASSubmitRsp = JsonUtil.fromJson(resString, MASSubmitRsp.class);
            return resMASSubmitRsp;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 移动-云MAS-模板短信消息推送
     *
     * @return
     */
    public static MASSubmitRsp sendSMSMASHttpTmpSubmit(String mobiles, String templateId, String[] params) {
        String apiUrl = PropertyUtil.getString("http.api.sms.mas.tmpsubmit");
        String apId = PropertyUtil.getString("sms.mas.apId");
        String ecName = PropertyUtil.getString("sms.mas.ecName");
        String secretKey = PropertyUtil.getString("sms.mas.secretKey");
        String sign = PropertyUtil.getString("sms.mas.sign");
        String addSerial = PropertyUtil.getString("sms.mas.addSerial");
        MASTmpSubmitReq tmpSubmitReq = new MASTmpSubmitReq();
        tmpSubmitReq.setApId(apId);
        tmpSubmitReq.setEcName(ecName);
        tmpSubmitReq.setSecretKey(secretKey);
        tmpSubmitReq.setParams(JsonUtil.toJson(params));
        tmpSubmitReq.setMobiles(mobiles);
        tmpSubmitReq.setAddSerial(addSerial);
        tmpSubmitReq.setSign(sign);
        tmpSubmitReq.setTemplateId(templateId);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(tmpSubmitReq.getEcName());
        stringBuffer.append(tmpSubmitReq.getApId());
        stringBuffer.append(tmpSubmitReq.getSecretKey());
        stringBuffer.append(tmpSubmitReq.getTemplateId());
        stringBuffer.append(tmpSubmitReq.getMobiles());
        stringBuffer.append(tmpSubmitReq.getParams());
        stringBuffer.append(tmpSubmitReq.getSign());
        stringBuffer.append(tmpSubmitReq.getAddSerial());
        tmpSubmitReq.setMac(EncryptUtil.md5(stringBuffer.toString()));
        // 参数转成json字符串
        String reqText = JsonUtil.toJson(tmpSubmitReq);
        // 参数转成base64
        String encode = Base64.encodeBase64String(reqText.getBytes());
        try {
            String resString = HttpClientUtil.doPostWithJson(apiUrl, encode);
            MASSubmitRsp resMASSubmitRsp = JsonUtil.fromJson(resString, MASSubmitRsp.class);
            return resMASSubmitRsp;
        } catch (Exception ex) {
            return null;
        }
    }

    /**
     * 生成随机六位数验证码
     *
     * @return
     */
    public static String generateRandomVcode() {
        //验证码
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int) (Math.random() * 10);
        }
        return vcode;
    }
}
